package com.kool.system.action.roleGroup;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kool.core.base.BaseBean;
import com.kool.core.base.IBaseAction;
import com.kool.core.exception.AppException;
import com.kool.core.util.BeanUtils;
import com.kool.core.util.GlobalDefineUtils;
import com.kool.system.bean.SyRoleGroupBean;
import com.kool.system.bean.io.SYPAGEINFOYBean;
import com.kool.system.bean.io.roleGroup.SYROLEGROUPINFYBean;
import com.kool.system.bean.vo.PageResult;
import com.kool.system.service.RoleGroupService;

/**
 * @DESCRIBE 分页查询
 * @AUTHOR LUYU
 */
@Controller
public class QueryPageRoleGroupAction implements IBaseAction {
	@Autowired
	private RoleGroupService service;

	@ResponseBody
	@RequestMapping(value = "/sys/roleGroup/queryRoleGroupListPage", method = RequestMethod.POST)
	public JSONObject doAction(@RequestBody String sInput) throws AppException {
		JSONObject pkgIn = JSONObject.parseObject(sInput);
		JSONArray secIn = pkgIn.getJSONArray("SYROLEGROUPINFY");
		SyRoleGroupBean beanIn = null;
		if (secIn != null && secIn.size() > 0) {
			JSONObject propIn = secIn.getJSONObject(0);
			SYROLEGROUPINFYBean ioIn = JSON.toJavaObject(propIn, SYROLEGROUPINFYBean.class);
			beanIn = (SyRoleGroupBean) BeanUtils.io2Bean(ioIn, SyRoleGroupBean.class, "srg");
		} else {
			beanIn = new SyRoleGroupBean();
		}

		JSONArray sectionInPageInfo = pkgIn.getJSONArray("SYPAGEINFOY");
		JSONObject propInPageInfo = sectionInPageInfo.getJSONObject(0);
		SYPAGEINFOYBean pageInfo = JSONObject.toJavaObject(propInPageInfo, SYPAGEINFOYBean.class);

		PageResult pageResult = service.queryPage(beanIn, pageInfo);
		JSONObject pkgOut = new JSONObject();
		JSONArray secOut = new JSONArray();
		for (BaseBean syRoleGroup : pageResult.getListBusinessValue()) {
			SYROLEGROUPINFYBean ioOut = (SYROLEGROUPINFYBean) BeanUtils.bean2Io(syRoleGroup, SYROLEGROUPINFYBean.class);
			JSONObject propOut = (JSONObject) JSON.toJSON(ioOut);
			GlobalDefineUtils.addTransformation(propOut, "state", "SRG_STATE");
			secOut.add(propOut);
		}
		pkgOut.put("SYROLEGROUPINFY", secOut);

		JSONArray sectionOutPageInfo = new JSONArray();
		pageInfo = pageResult.getPageInfo();
		JSONObject propOutPageInfo = JSONObject.parseObject(JSONObject.toJSONString(pageInfo));
		sectionOutPageInfo.add(propOutPageInfo);
		pkgOut.put("SYPAGEINFOY", sectionOutPageInfo);
		return pkgOut;
	}

}
